<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        ul {
            list-style: none;
        }

        .box {
            width: 500px;
            margin: 20px auto;
        }

        ul {
            position: relative;
            top: 1px;
            display: flex;

            line-height: 35px;
        }

        ul li {
            padding: 0 10px;
            margin-right: 20px;
            border: 1px solid black;
        }

        .box>div {
            display: none;
            padding: 10px;
            height: 150px;
            border: 1px solid black;
        }

        .box>div.active {
            display: block;
        }

        ul li.active {
            border-bottom-color: #fff;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <li class="active">编程</li>
            <li>读书</li>
            <li>运动</li>
        </ul>
        <div class="active">编程使我快乐</div>
        <div>读书使我幸福</div>
        <div>运动使我健康</div>
    </div>
    <script>
        let ul = document.querySelector('ul')
        let lis = document.querySelectorAll('li')
        let box = document.querySelector('.box').querySelectorAll('div')

        //自执行函数方案
        for (var i = 0; i < lis.length; i++) {
            lis[i].onclick = (function (i) {
                return function () {
                    getlis(i)
                }
            })(i)
        }
        function getlis(index) {
            for (let i = 0; i < lis.length; i++) {
                lis[i].className = ''
                box[i].className = ''
            }
            lis[index].className = 'active'
            box[index].className = 'active'

        }
    </script>
</body>

</html>